//Grab pas utilise, cause : restriction firewall
//@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2' )
//import org.ccil.cowan.tagsoup.Parser
import org.apache.poi.hssf.usermodel.*
import org.apache.poi.ss.usermodel.*
import org.apache.poi.hssf.util.*
import java.io.File
import org.apache.poi.ss.util.CellRangeAddress;

def conf = new ConfigSlurper().parse(new File("C:/Users/gl05c06a/Documents/=-NavFacilities-=/helpers/config.groovy").toURL())
conf.options.each{
    println "Traitement des liens href de ${it.value.xlsInput} ..."
    doExtractLink(it.value.xlsInput, it.value.xlsOutput, it.value.sheetIndex, it.value.linksInputColumnIndex, it.value.linksOutputColumnIndex)
}

def doExtractLink(xlsInput, xlsOutput, sheetIndex, linksInputColumnIndex, linksOutputColumnIndex){
    def myxls = new FileInputStream(xlsInput)
    def wb = new HSSFWorkbook(myxls)
    def HSSFSheet sheet = wb.getSheetAt(sheetIndex);
    def rowIterator = sheet.rowIterator()
    while(rowIterator.hasNext()) {
        row = rowIterator.next()
        def cell = row.getCell(linksInputColumnIndex)
        if(null != cell){
            Hyperlink link = cell.getHyperlink();
            if(link != null){
                cell = row.createCell(linksOutputColumnIndex)
                cell.setCellValue(link.getAddress());
            }
        }
    }
    FileOutputStream out = new FileOutputStream(xlsOutput);
    wb.write(out);
    out.close();
    myxls.close();
}